//
//  YZXNewFeatCell.swift
//  YZXWeibo
//
//  Created by 叶周翔 on 16/7/22.
//  Copyright © 2016年 叶周翔. All rights reserved.
//

import UIKit

class YZXNewFeatCell: UICollectionViewCell {
    
    var startBtnDidCallBack:(() ->Void)?
    
    /// 外界传入一个图片来给cell设置图片
    var image: UIImage?{
        didSet{
            bgImage.image = image
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setUpUI(){
        self.contentView.addSubview(bgImage)
        self.contentView.addSubview(startBtn)
        self.contentView.addSubview(shareBtn)
        
        // 设置约束
        // 背景的约束
        bgImage.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.contentView)
        }
        // 分享按钮
        shareBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.contentView)
            make.bottom.equalTo(self.contentView).offset(-160)
        }
        // 开始按钮
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.contentView)
            make.top.equalTo(shareBtn.snp_bottom).offset(16)
        }
        
    }
    
    // MARK:- 懒加载
    lazy var bgImage: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    // 分享给大家
    lazy var shareBtn:UIButton = {
        let btn = UIButton()
        btn.setTitle("分享给大家", forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "new_feature_share_false"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "new_feature_share_true"), forState: UIControlState.Selected)
        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        // 添加点击事件
        btn.addTarget(self, action: "shareToOther", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    // 开始体验按钮
    lazy var startBtn:UIButton = {
        let btn = UIButton()
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Highlighted)
        
        // 添加点击事件
        btn.addTarget(self, action: "startBtnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
}

extension YZXNewFeatCell{
    // 分享按钮的点击
    func shareToOther(){
        self.shareBtn.selected = !self.shareBtn.selected
        
    }
    // 开始使用按钮的点击
    func startBtnDidClick(){
        startBtnDidCallBack?()
    }
    
    // 在最后一个item中显示分享和开始按钮
    func showBtn(isShow:Bool) {
        if isShow{
            self.shareBtn.hidden = true
            self.startBtn.hidden = true
        }else{
            self.startBtn.hidden = false
            self.shareBtn.hidden = false
        }
    }
}



